﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Diagnostics;
using DevExpress.Utils;
using VSS_AHT.Properties;
using VSSAHT;
using System.IO;
namespace VSS_AHT
{
    public partial class FormCompanyInfor : BaseForm
    {
        #region Declare Avalibles
        private readonly CompanyInfo _company = new CompanyInfo();
        #endregion

        #region Singleton Members
        private static volatile FormCompanyInfor instance;
        private static object syncRoot = new Object();

        public static FormCompanyInfor Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormCompanyInfor();
                }

                return instance;
            }
        }
        #endregion

        #region Ctor
       
        public FormCompanyInfor()
        {
            InitializeComponent();
            Initialize();
        }

        public void Initialize()
        {
            txtCompanyName.Text = _company.Name;
            txtCompanyAddress.Text = _company.Address;
            txtFolder.Text = _company.StorePath;
            txtP.Text = _company.VehicleStation;

            this.btnSave.Click += new EventHandler(delegate { ClickButtonSave(); });
            this.btnFolder.Click +=new EventHandler(btnFolder_Click);
            this.btnCancel.Click += new EventHandler(delegate
                {
                    this.Close();

                });
            this.Disposed += new EventHandler(delegate { instance = null; });
        }
        
        #endregion

        #region Methods
        private void btnFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Chọn thư mục lưu trữ phiếu cân mặc định.";
            folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            folderDialog.ShowNewFolderButton = true;
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderDialog.SelectedPath + @"\";
            }
        }
        #endregion

        #region Events

        public void ClickButtonSave()
        {
            try
            {

                _company.Name = txtCompanyName.Text;
                _company.Address = txtCompanyAddress.Text;
                _company.StorePath = txtFolder.Text;
                _company.VehicleStation = txtP.Text;
                Settings.Default.Save();
             // if (!Directory.Exists(Common.storagePath))
                  //Directory.CreateDirectory(Common.storagePath);
                if (!Directory.Exists(_company.StorePath + "/0"))
                    Directory.CreateDirectory(_company.StorePath + "/0");
                if (!Directory.Exists(_company.StorePath + "/1"))
                    Directory.CreateDirectory(_company.StorePath + "/1");
              
                Common.ShowSuccessful("Lưu cấu hình thành công!");
            }
            catch (Exception ex)
            {
                Common.ShowAlert("Lưu cấu hình thất bại => ERROR! " + ex.Message);
            }
        }
        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            Settings.Default.Reload();
            base.OnClosed(e);
        }
        #endregion
    }
}